<?php

// +----------------------------------------------------------------------
// | Static Plugin for ThinkAdmin
// +----------------------------------------------------------------------
// | 版权所有 2014~2023 ThinkAdmin [ thinkadmin.top ]
// +----------------------------------------------------------------------
// | 官方网站: https://thinkadmin.top
// +----------------------------------------------------------------------
// | 开源协议 ( https://mit-license.org )
// | 免责声明 ( https://thinkadmin.top/disclaimer )
// +----------------------------------------------------------------------
// | gitee 代码仓库：https://gitee.com/zoujingli/think-plugs-static
// | github 代码仓库：https://github.com/zoujingli/think-plugs-static
// +----------------------------------------------------------------------

namespace app\api\controller;

use app\service\PyService;
use think\facade\Db;
use think\facade\Log;
use think\facade\Request;
use think\Response;
use app\service\WechatMiniProgramService;

class WxBoxController extends BaseController
{
    public function indexList()
    {
        $content = $this->request->post('content', '');
        $type = $this->request->post('type', 1);
//        if(empty($content)){
//            return jsonFail('内容不能为空');
//        }
        $commonTools = DB::table('xj_wxbox_clazz')->field('id,title,icon,url,sort')->where('pid',1)->select();
        $data = [
            'commonTools' =>$commonTools
        ];
        return jsonSuccess('ok',$data);
    }

    //小程序去水印
    public function getVideo(){
        $url = $this->request->post('url', '');
        if(empty($url)){
            return jsonFail('地址不能为空');
        }
        $info = (new PyService())->getVideo($url);
        return jsonSuccess('ok',$info);
    }

    public function downloadVideo(){
        $key = $this->request->post('key', '');
        if(empty($key)){
            return jsonFail('key不能为空');
        }
        $info = (new PyService())->downloadVideo($key);
        return jsonSuccess('ok',$info);
    }


    //反馈
    public function feedback(){
        $title = $this->request->post('title', '');
        $content = $this->request->post('content', '');
        if(empty($title)){
            return jsonFail('标题不能为空');
        }
        if(empty($content)){
            return jsonFail('内容不能为空');
        }
        $info = [
            'title' =>$title,
            'content' =>$content,
            'addtime' =>date('Y-m-d H:i:s'),
        ];
        DB::table('box_feedback')->insertGetId($info);
        return jsonSuccess('ok',$info);
    }


    //翻译
    public function translate(){
        $info = (new PyService())->translate();
        return jsonSuccess('ok',$info);
    }



    /**
     * 微信小程序登录
     */
    public function login()
    {
        try {
            $code = Request::param('code');
            $encryptedData = Request::param('encryptedData');
            $iv = Request::param('iv');
            $rawData = Request::param('rawData');

            $userData = [];
            if ($encryptedData && $iv) {
                $userData['encryptedData'] = $encryptedData;
                $userData['iv'] = $iv;
            }
            if ($rawData) {
                $userData['rawData'] = $rawData;
            }

            $service = new WechatMiniProgramService();
            $result = $service->login($code, $userData);
            return jsonSuccess('ok',$result);
        } catch (\Exception $e) {
            return jsonFail($e->getMessage());
        }
    }

    /**
     * 刷新Token
     */
    public function refreshToken()
    {
        try {
            $token = Request::header('Api-Token') ?? Request::param('token');

            $service = new WechatMiniProgramService();
            $result = $service->refreshToken($token);

            if (!$result) {
                throw new \Exception('Token刷新失败');
            }

            return json([
                'code' => 1,
                'msg' => '刷新成功',
                'data' => $result
            ]);

        } catch (\Exception $e) {
            return json([
                'code' => 0,
                'msg' => $e->getMessage(),
                'data' => []
            ]);
        }
    }

    /**
     * 注销登录
     */
    public function logout()
    {
        try {
            $token = Request::header('Api-Token') ?? Request::param('token');

            $service = new WechatMiniProgramService();
            $result = $service->logout($token);

            return json([
                'code' => 1,
                'msg' => '注销成功',
                'data' => []
            ]);

        } catch (\Exception $e) {
            return json([
                'code' => 0,
                'msg' => $e->getMessage(),
                'data' => []
            ]);
        }
    }

}
